﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data.Providers;
using System.Data;
using System.Data.SqlClient;
using ARR.Entities.Gateways;
using DDF.Data;

namespace ARR.DataModel.Repositories
{
    public static partial class TempInventoriesRepository
    {
        public static void TruncateTable()
        {
            using (SqlDataProvider database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Timeout = 900;
                database.NewCommand("TRUNCATE TABLE TempInventories ", System.Data.CommandType.Text);

                database.ExecuteNonQuery();
            }
        }

        public static void BulkCopy(DataTable inventoriesToCopy)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                using (SqlBulkCopy bulk = new SqlBulkCopy(conn))
                {
                    bulk.DestinationTableName = "TempInventories";
                    bulk.BulkCopyTimeout = 2000;
                    bulk.WriteToServer(inventoriesToCopy);
                    bulk.Close();
                }

                conn.Close();
            }
        }

        public static void UpdateIds(int rangeReviewId)
        {
            using (SqlDataProvider database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Timeout = 2000;
                database.NewCommand("TempInventory_UpdateIds", CommandType.StoredProcedure);
                database.AddInParameter("RangeReviewId", DbType.Int32, rangeReviewId);
                database.ExecuteNonQuery();
            }
        }

        public static List<ArticleRow> SelectNewArticles(int rangeReviewId)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Timeout = 900;
                database.NewCommand("TempInventories_SelectNewArticles", CommandType.StoredProcedure);
                database.AddInParameter("RangeReviewId", DbType.Int32, rangeReviewId);
                IDataReader reader = database.ExecuteReader();

                List<ArticleRow> articles = new List<ArticleRow>();

                LoadArticles(reader, articles);

                return articles;
            }
        }

        private static void LoadArticles(IDataReader reader, List<ArticleRow> articles)
        {
            ArticleRow row;

            while (reader.Read())
            {
                row = new ArticleRow()
                {
                    CategoryId = Util.CastDbValue<int>(reader["CategoryId"]),
                    CategoryCode = Util.CastDbValue<string>(reader["CategoryCode"]),
                    Code = Util.CastDbValue<string>(reader["ArticleCode"]),
                    Name = Util.CastDbValue<string>(reader["Description"]),
                    EAN = Util.CastDbValue<string>(reader["EAN"]),
                    Brand = Util.CastDbValue<string>(reader["Brand"]),
                    Manufacturer = Util.CastDbValue<string>(reader["Manufacturer"])
                };

                articles.Add(row);
            }

            reader.Close();
        }
    }
}
